package com.lovo.boot.service.impl;

import com.lovo.boot.dao.IUserDao;
import com.lovo.boot.entity.UserEntity;
import com.lovo.boot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    static List<UserEntity> uList = new ArrayList<>();
    static {
        uList.add(new UserEntity(1,"张三",20,66));
        uList.add(new UserEntity(2,"李四",22,69));
        uList.add(new UserEntity(3,"王五",23,56));
        uList.add(new UserEntity(4,"赵六",24,88));
        uList.add(new UserEntity(5,"宋琦",22,79));
        uList.add(new UserEntity(6,"王八",21,95));
    }

    @Override
    public List<UserEntity> del(String name) {
        this.delBy(name);
        return this.findAll();
    }

    @Override
    public List<UserEntity> findUserAll() {
        return uList;
    }

    @Override
    public List<UserEntity> findByGrade(int startGrade, int endGrade) {
        List<UserEntity> list = new ArrayList<>();
        for (int i = 0; i < uList.size(); i++) {
            UserEntity userEntity = uList.get(i);
            if(userEntity.getGrade()>=startGrade && userEntity.getGrade()<=endGrade){
                list.add(userEntity);
            }
        }
        return list;
    }

    public void delBy(String name) {
        for (int i = 0; i < uList.size(); i++) {
            if(name != null && name.equals(uList.get(i).getUserName())){
                uList.remove(i);
                return;
            }
        }
    }

    public List<UserEntity> findAll() {
        return uList;
    }
}
